<?php
session_start();
include_once("./configuration.php");
/* This is what happens from "preview" -button. At first we generate temporary filter list */
//var_dump($_POST);
if($_SESSION['logged']) {
	$ignore_file = $_SESSION['homedir'].$_POST['filename'].".filter.txt";
	$anonymize_file = $_SESSION['homedir'].$_POST['filename'].".anonymize.txt";
//	$ignore = fopen($ignore_file, "a");
	$level = escapeshellarg($_POST['level']);
//	$ano = fopen($anonymize_file, "a");
	$ignore_array = array();
	if(file_exists($ignore_file))
          $ignore_array = file($ignore_file);
	$anonymize_array= array();
	if(file_exists($anonymize_file))
  	  $anonymize_array = file($anonymize_file);
	foreach($_POST as $key=>$value) {
		$key = $key."\n";
		if($value == "ignore") {
			/* strtolower doesn't handle following characters .. */
			$key = str_replace("Ä","ä", $key);
			$key = str_replace("Ö","ö", $key);
			$key = str_replace("Å","å", $key);
			if(in_array($key, $anonymize_array)) {
			  $k = array_search($key, $anonymize_array);
			  if(isset($anonymize_array[$k]))
			    unset($anonymize_array[$k]);
			  $k = array_search(strtolower($key), $anonymize_array);
			  if(isset($anonymize_array[$k]))
		   	    unset($anonymize_array[$k]);	
			}
			array_push($ignore_array, $key);
			$key = strtolower($key);
			array_push($ignore_array, $key);
		}
		if($value == "delete") {
			/* strtolower doesn't handle following characters .. */
			$key = str_replace("Ä","ä", $key);
			$key = str_replace("Ö","ö", $key);
			$key = str_replace("Å","å", $key);
			if(in_array($key, $ignore_array)) {
			  $k = array_search($key, $ignore_array);
			  if(isset($ignore_array[$k]))
			    unset($ignore_array[$k]);
			  $k = array_search(strtolower($key), $ignore_array);
			  if(isset($ignore_array[$k]))
		   	    unset($ignore_array[$k]);	
			}
			array_push($anonymize_array, $key);
			$key = strtolower($key);
			array_push($anonymize_array, $key);
		}
		if(preg_match("/new_entry/", $key)) {
		  if(strlen($value) > 1) { 
		   array_push($anonymize_array, $value."\n");
		   array_push($anonymize_array, strtolower($value."\n"));
			if(in_array($value."\n", $ignore_array)) {
			  $k = array_search($value."\n", $ignore_array);
			  if(isset($ignore_array[$k]))
			    unset($ignore_array[$k]);
			  $k = array_search(strtolower($value."\n"), $ignore_array);
			  if(isset($ignore_array[$k]))
		   	    unset($ignore_array[$k]);	
			}
		  }
		}
		
	}
	$anonymize_array = array_unique($anonymize_array);
	$ignore_array = array_unique($ignore_array);

	$ignore_handle = fopen($ignore_file, "w");
	$anonymize_handle = fopen($anonymize_file, "w");
	foreach($ignore_array as $n) { fwrite($ignore_handle, $n); }
	foreach($anonymize_array as $n) { fwrite($anonymize_handle, $n); }
	fclose($ignore_handle); 
	fclose($anonymize_handle);

	$uploads = $_SESSION['uploads'];
	$processed = $_SESSION['processed'];

	$javadir = dirname(__FILE__)."/logic/";
	$cmd = "cd $javadir && export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.21 ; java -jar logic.jar -input ".$uploads."".$_POST['filename']." -level 2 -ignore ".escapeshellarg($ignore_file)." -anonymize ".escapeshellarg($anonymize_file)." -output ".$processed."".escapeshellarg($_POST['filename'].".html");
	shell_exec($cmd); 
//print $cmd;
}
header("Location: ./index.php?p=edit&file=".$_POST['filename']);
?>
